import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.IOException;
import java.util.TreeSet;

public class LightControl {


        public static void main(String[] args) {

            //创建一个白板窗口
            JFrame frame = new JFrame("Brightness Control");

            //获得一个滑动干 并初始话滑动杠 最小0 最大100 初始值50
            JSlider brightnessSlider = new JSlider(0, 100, 50);

            //获得一个标签 初始表签
            JLabel brightnessLabel = new JLabel("Brightness: 50");

            //设置主键个空隙 下方显示的数字之间的间隙
            brightnessSlider.setMajorTickSpacing(10);

            //设置最小刻度间隔空隙
//            brightnessSlider.setMinorTickSpacing(5);

            //是否需要刻度标记
            brightnessSlider.setPaintTicks(false);

            //滑条下是否需要数字标记
            brightnessSlider.setPaintLabels(true);

            //获得滑块的值
            brightnessSlider.addChangeListener(new ChangeListener() {


                @Override

                public void stateChanged(ChangeEvent e) {

                    //获得滑块对应的值
                    int brightness = brightnessSlider.getValue();

                    //更改滑条下方显示的值
                    brightnessLabel.setText("Brightness: " + brightness);

                    // 在这里可以添加实际的亮度调节代码
                    RunToCmd(brightness);

                }


            });

            //面板
            JPanel panel = new JPanel();

            //为面板加入滑块
            panel.add(brightnessSlider);

            //为面板加入标签
            panel.add(brightnessLabel);

            //最终添加到面板中
            frame.add(panel);

            //面板初始大小
            frame.setSize(300, 200);

            //默认退出方式
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //窗口可见
            frame.setVisible(true);

            //窗口的初始位置
            frame.setLocation(1100,550);

        }

    private static void RunToCmd(int brightness) {
        String instruct = "WMIC /NAMESPACE:\\\\root\\wmi PATH WmiMonitorBrightnessMethods WHERE \"Active=TRUE\" CALL WmiSetBrightness Brightness="+ brightness+" Timeout=0";
//        System.out.println(instruct);
        //执行cmd命令
        try {
//            System.out.println(instruct);
            Runtime.getRuntime().exec(instruct);
        } catch (IOException io) {
            io.getLocalizedMessage();
        }
    }


}
